/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListaClientes.java
 *
 * Created on 08-abr-2009, 11:04:00
 */

package laboratorio;

import clientes.Cliente;
import clientes.Doctor;

/**
 *
 * @author rocio
 */
public class ListaClientes extends javax.swing.JFrame {
    private Cliente clienteSeleccionado;
    private Clientes VentanaClientesOld;
    private Doctores VentanaDoctoresOld;
    private ResumenFacturacion VentanaResumenOld;
    private Trabajos VentanaTrabajosOld;
    private Doctor doctorSeleccionado;
    private String tipo;
    private Presupuestos VentanaPresupuestoOld;
    /** Creates new form ListaClientes */
    public ListaClientes() {
        initComponents();
    }

    public ListaClientes(Clientes clientesOld) {
        VentanaClientesOld = clientesOld;
        initComponents();
        jButton3.setVisible(false);
        tipo="clientes";
    }

    public ListaClientes(Doctores aThis) {
        VentanaDoctoresOld=aThis;
        initComponents();
        tipo ="doctores";
    }
     public ListaClientes (ResumenFacturacion resFac){
         VentanaResumenOld=resFac;
         initComponents();
         jButton3.setVisible(false);
         tipo="resumenFac";
     }

    public ListaClientes(Presupuestos aThis) {
         VentanaPresupuestoOld=aThis;
         initComponents();
         tipo="presupuestos";
    }

    public ListaClientes(Trabajos aThis) {
        VentanaTrabajosOld=aThis;
        initComponents();
        tipo="trabajos";
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        lpdPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("lpdPU").createEntityManager();
        clienteQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT c FROM Cliente c");
        clienteList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : clienteQuery.getResultList();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(laboratorio.LaboratorioApp.class).getContext().getResourceMap(ListaClientes.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Salir(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setName("jTable1"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, clienteList, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${IDcliente}"));
        columnBinding.setColumnName("IDcliente");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nombre}"));
        columnBinding.setColumnName("Nombre");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                seleccionarCliente(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTable1.columnModel.title0")); // NOI18N
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable1.columnModel.title1")); // NOI18N

        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Salir(evt);
            }
        });

        jButton2.setFont(resourceMap.getFont("jButton2.font")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BotonSeleccionar(evt);
            }
        });

        jButton3.setFont(resourceMap.getFont("jButton3.font")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nuevoCliente(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void Salir(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Salir

       clienteSeleccionado=null;
       this.setVisible(false);
    }//GEN-LAST:event_Salir
    public Cliente getCliente(){
        return clienteSeleccionado;
    }
    private void seleccionarCliente(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_seleccionarCliente
     //TODO
        int row=jTable1.getSelectedRow();
        clienteSeleccionado=clienteList.get(row);


     

    }//GEN-LAST:event_seleccionarCliente

    private void BotonSeleccionar(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BotonSeleccionar

        this.setVisible(false);
        if(tipo.equals("clientes")){
            VentanaClientesOld.setClienteSelec(clienteSeleccionado);
        }else if(tipo.equals("doctores")){
            VentanaDoctoresOld.setCliente(clienteSeleccionado);
        }else if (tipo.equals("resumenFac")){
            VentanaResumenOld.setCliente(clienteSeleccionado);
        }else if(tipo.equals("trabajos")){
            VentanaTrabajosOld.setCliente(clienteSeleccionado);
        }else if(tipo.equals("presupuestos")){
            VentanaPresupuestoOld.setClienteSelect(clienteSeleccionado);
        }

}//GEN-LAST:event_BotonSeleccionar

    private void nuevoCliente(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nuevoCliente
        Clientes c=new Clientes();
        c.nuevo();
        c.setVisible(true);
        
    }//GEN-LAST:event_nuevoCliente

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ListaClientes().setVisible(true);
            }
        });
    }
    public Clientes clientes =new Clientes();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<clientes.Cliente> clienteList;
    private javax.persistence.Query clienteQuery;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.persistence.EntityManager lpdPUEntityManager;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
